import Router from "umi/router";

import {httpUtilConfig} from 'dcloud-core';
import fetchJsonp from 'fetch-jsonp';

class loginServer{
    constructor(url, parmas, method, headers) {

    }

}

/**
* 跳转至登录页
* @param flag: boolean
*/
const goLogin = (flag = true) => {
    if(window.location.hash == '#/login'){
        sessionStorage.setItem('refUrl', '#/main')
    }else{
        sessionStorage.setItem('refUrl', window.location.hash)
    }

    // 有页面跳转时, 使用replace跳转到登录页
    if(flag){
        Router.replace('/login')
    }else{
        // 无页面跳转时, 使用push跳转到登录页
        Router.push('/login')
    }
}

/**
 * 账号密码登录方法
 *
 */
const login = () => {
    let refUrl
    if(localStorage.getItem('refUrl')){
        if(localStorage.getItem('refUrl') == '#/login'){
            localStorage.setItem('refUrl', '#/main')
        }
        refUrl = localStorage.getItem('refUrl')
    }else{
        localStorage.setItem('refUrl', '#/main')
        refUrl = localStorage.getItem('refUrl')
    }
    let url = refUrl.replace("#",'');
    Router.replace(url);
}

/**
 * 微信一键登录
 *
 */
const goAuth = () => {
    if(window.location.hash == '#/login'){
        sessionStorage.setItem('refUrl', '#/main')
    }else{
        sessionStorage.setItem('refUrl', window.location.hash)
    }

    Router.replace('/login/auth')
}

/**
 * 登出方法
 * @param url: String  登出后的页面路由
 */
const logout = (url) => {
    sessionStorage.clear();
    Router.push(url)
}

export default {
    goLogin,
    login,
    logout,
    goAuth
}





